package org.grow.mpgo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.grow.mpgo.dao.DealBillDao;
import org.grow.mpgo.dao.StudentScoreDao;

import org.grow.mpgo.entity.*;
import org.grow.mpgo.exception.TuesdayException;

import org.grow.mpgo.service.DealBillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author: xwg
 * @CreateDate: 21-7-12
 */
@Slf4j
@RestController
public class StudentScoreController {
    //    Logger logger = LoggerFactory.getLogger(StudentScoreController.class);
    @Autowired
    private StudentScoreDao studentScoreDao;

    @PutMapping("/studentScore")
    public Boolean insert(@RequestBody StudentScore studentScore) {
        System.out.println("普通打印" + studentScore);
        log.warn(studentScore.toString());
//        logger.warn();
        int insert = studentScoreDao.insert(studentScore);
        return insert == 1;
    }

    @PostMapping("/studentScore/query")
    public PagedResults<StudentScore> query(
            @RequestBody ScorePagedCondition scorePagedCondition) {
        Integer pageNum = scorePagedCondition.getPageNum();
        if (pageNum <= 0) {

            throw new TuesdayException("pageNum不能小于1");
        }
        Integer pageSize = scorePagedCondition.getPageSize();
        if (pageSize <= 0) {
            throw new TuesdayException("数量不能小于0", 888);
        }
        ScoreQueryCondition qc = scorePagedCondition.getQueryCondition();

        String similarName = qc.getSimilarName();
        if (similarName.contains("张")) {
            throw new TuesdayException("用户不能姓张");
        }
        Page<StudentScore> page = new Page<>(pageNum, pageSize);
        IPage<StudentScore> iPage = studentScoreDao.dynamicQueryPaged(page, qc);

        PagedResults<StudentScore> pagedResults = new PagedResults<>();
        pagedResults.setTotal(iPage.getTotal());
        pagedResults.setResults(iPage.getRecords());
        return pagedResults;
    }

    @Autowired
    private DealBillDao dealBillDao;
    @Autowired
    private DealBillService dealBillService;

    @PostMapping("/studentScore/deal")
    public Boolean deal(@RequestBody DealBill dealBill) {

        dealBillDao.insert(dealBill);
        System.out.println("拥有id的 ： " + dealBill);
        String consumerName = dealBill.getConsumerName();
        String supplierName = dealBill.getSupplierName();
//        studentScoreDao
        StudentScore consumer = studentScoreDao.findByName(consumerName);
        StudentScore supplier = studentScoreDao.findByName(supplierName);
        System.out.println("consumer ;" + consumer);
        System.out.println("supplier ;" + supplier);


        consumer.setOnlineScore(consumer.getOnlineScore() + dealBill.getDealScore());
        supplier.setOnlineScore(supplier.getOnlineScore() - dealBill.getDealScore());


        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return dealBillService.txDeal(consumer,supplier);
//        int i = studentScoreDao.updateById(consumer);
//        if (1 == 1) throw new RuntimeException("模拟断电了");
//        int j = studentScoreDao.updateById(supplier);
//        return (i == 1 && j == 1);

//        return  this.txDeal(consumer,supplier);
    }
//    @Transactional
//    private boolean txDeal(StudentScore consumer,StudentScore supplier){
//        int i = studentScoreDao.updateById(consumer);
//        if (1==1) throw new RuntimeException("模拟断电了");
//        int j = studentScoreDao.updateById(supplier);
//        return (i==1 && j==1);
//
//    }


}
